package com.campus.academic.controller;


import com.campus.academic.job.dto.*;
import com.campus.academic.job.entity.LearningResources;
import com.campus.academic.job.entity.News;
import com.campus.academic.service.ILearningResourcesService;
import com.campus.academic.service.INewsService;
import com.campus.common.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 新闻表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-16
 */
@RestController
@RequestMapping("/news")
@Tag(name = "校园新闻服务")
public class NewsController {

    @Autowired
    private INewsService newsService;

    @ApiOperation(value = "发布新闻")
    @PostMapping()
    public Result<Void> publishNews(@RequestBody NewsDTO newsDTO) {

        newsService.saveNews(newsDTO);
        return Result.success();
    }

    @ApiOperation(value = "修改新闻信息")
    @PutMapping()
    public Result<Void> updateNews(@RequestBody NewsUpdateDTO newsUpdateDTO) {
        newsService.updateNews(newsUpdateDTO);
        return Result.success();
    }


    @ApiOperation(value = "删除新闻信息")
    @DeleteMapping()
    public Result<Void> deleteNews(@RequestParam("id") Integer id) {
        newsService.deleteNews(id);
        return Result.success();
    }

    @ApiOperation(value = "查询新闻信息")
    @GetMapping()
    public Result<News> getNews(@RequestParam("id") Integer id) {
        return Result.success(newsService.getById(id));
    }

    @ApiOperation(value = "获取新闻信息列表")
    @GetMapping("/list")
    public Result<List<News>> getNewsList() {
        List<News> list = newsService.getNewsList();
        return Result.success(list);
    }

    @ApiOperation(value = "审核新闻")
    @PostMapping("/audit")
    public Result<Void> auditNews(@RequestBody AuditNewsDTO auditNewsDTO) {
        newsService.auditNews(auditNewsDTO);
        return Result.success();
    }

    @ApiOperation(value = "上传图片信息")
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam MultipartFile file) throws IOException {
        String url = newsService.uploadImage(file);
        System.out.println("上传图片成功，url:"+url);
        return Result.success(url);
    }

}
